<?php
//ini_set ('error_reporting', E_ALL);


class Admin_GalleryController extends Zend_Controller_Action
{
 
    public function init()
    {
		/* Initialize action controller here */
    }
	
   public function indexAction()
   {
		
	}	
	
/////////////////////////////////////////////////////////////////////////////////
	
   public function addAction()
	{
		$galleryFacade = new Admin_Model_Facade_Gallery(); 		
		$translate = Zend_Registry::get('Zend_Translate');
		$ToGallery = new Admin_Model_TO_Gallery();
		$s_Sass = new Zend_Session_Namespace('s_Sass');
       
        
		$request  = $this->getRequest();                
      $idUser = $s_Sass->UserId;
      $company = $s_Sass->CompanyId;
        
      $request  = $this->getRequest();
		$x_id  = $this->_request->getParam('Id');
		
		
		$form = new Admin_Form_GalleryAdd();
		$form->setAction(Zend_Controller_Front::getInstance()->getBaseUrl() . DIAG_PATH .$this->_request->getParam('module').DIAG_PATH.$this->_request->getParam('controller').DIAG_PATH.'add');
        
        if($request->isPost()){
            if($form->isValid($this->_request->getPost())){
				
				$ToGallery->setId($form->getValue('Id'));
				$ToGallery->setName($form->getValue('Name'));
				$ToGallery->setDescription($form->getValue('Description'));
				$ToGallery->setParent($form->getValue('Parent'));
				$ToGallery->setPrice($form->getValue('Price'));
				
				$ToGallery->setModified_By(isset($idUser) ? $idUser : 1);
				
				if($ToGallery->getId()==0){				
					
					$data = true; //$galleryFacade->createProduct($ToGallery->getAllVars());
					
					if($data){
						$file = $form->Image;		
						
						try {
		            	$id = '67';
		             }catch( Exception $e ) {
		             	$this
		             		->_helper
		             		->flashMessenger()
		               	->addMessage('Hubo un problema, revise los datos');
						}
						
						
						$upload = $form->Image->getTransferAdapter();
						$upload->addFilter('Rename', array(
		            	'target' => APPLICATION_PATH . '/../public/images/product_'. $id .'.jpg',
		               'overwrite' => true
					   ));
						$upload->receive();
						
						require_once (APPLICATION_PATH.'/plugins/phpthumb/ThumbLib.inc.php');
						$thumb = PhpThumbFactory::create(APPLICATION_PATH.'/../public/images/product_'. $id .'.jpg');
						$thumb->resize(100, 100)->save(APPLICATION_PATH.'/../public/images/product_mini_'. $id .'.jpg');
						$thumb->show();
						
						
						$s_Sass->message = $translate->_('Category added');					
						$this->_redirect($this->_request->getParam('module').DIAG_PATH.$this->_request->getParam('controller').DIAG_PATH.'list');
						
					}
					
				}
					
			}								
		}
		
		if($x_id>0 && is_numeric($x_id)){
			$ToCategory->setId($x_id);			
			$data=$categoryFacade->readCategory($ToCategory->getAllVars());
			
			$form = new Admin_Form_CategoryAdd($data);
			
			$this->view->module= $this->_request->getParam('module');
			$this->view->controller= $this->_request->getParam('controller');
			$form->Id->setValue($x_id);
			$this->view->form=$form;
			
		}
		else
		{	
			
			$form = new Admin_Form_GalleryAdd();
			$form->setAction(Zend_Controller_Front::getInstance()->getBaseUrl() . DIAG_PATH .$this->_request->getParam('module').DIAG_PATH.$this->_request->getParam('controller').DIAG_PATH.'add');
      	$form->setAttrib('enctype', 'multipart/form-data');
      	
			$this->view->module= $this->_request->getParam('module');
			$this->view->controller= $this->_request->getParam('controller');
			
			//////////////////////////
			$this->view->form=$form;
			
		}
   }
    
///////////////////////////////////////////////
	public function listAction(){
		
		$s_Sass = new Zend_Session_Namespace('s_Sass');
		$FacadeCategory = new Admin_Model_Facade_Category();
		$company=$s_Sass->CompanyId;
		
		$request  = $this->getRequest();
      
		if (strlen($s_Sass->message)>0)
		{
			$this->view->message = $s_Sass->message;
			$s_Sass->message="";
		}
		
		$data = $FacadeCategory->readAllCategory($company);
		
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($data));
		$paginator	->setItemCountPerPage(12)
					->setCurrentPageNumber($this->_getParam('page', 1));
		
		$this->view->paginator = $paginator;
		$this->view->module= $this->_request->getParam('module');
		$this->view->controller= $this->_request->getParam('controller');
		
	}
	
//////////////////////////////////////////////////////////    
    public function deleteAction()
	{
		
		$CategoryFacade=	new Admin_Model_Facade_Category();
		$ToCategory=		new Admin_Model_TO_Category();
		$translate = 		Zend_Registry::get('Zend_Translate');
		$s_Sass = 			new Zend_Session_Namespace('s_Sass');
		
		$request=$this->getRequest();
		$id=$this->_request->getParam('Id');
		$modifico =($s_Sass->UserId != 0)? $s_Sass->UserId : 1;
		
		$ToCategory->setId($id);
		$ToCategory->setModified_By($modifico);
		
		$data=$CategoryFacade->deleteCategory($ToCategory);
		if($data)
			$s_Sass->message = $translate->_('Category deleted.');
		
		$this->_redirect($this->_request->getParam('module').DIAG_PATH.$this->_request->getParam('controller').DIAG_PATH.'list');
		
    }	    
}


?>
